const express = require('express')
const cors = require('cors')
const app = express()
const port = 8080

// 启用CORS
app.use(cors())

// 解析JSON请求体
app.use(express.json())

// 测试连接接口
app.get('/test', (req, res) => {
  res.json({
    success: true,
    message: '后端服务器连接成功！',
    timestamp: new Date().toISOString(),
    server: 'Node.js Express Server'
  })
})

// 获取教室列表接口
app.get('/classrooms', (req, res) => {
  res.json({
    success: true,
    data: [
      { id: 1, name: '教室A101', capacity: 50, status: 'available' },
      { id: 2, name: '教室A102', capacity: 30, status: 'occupied' },
      { id: 3, name: '教室B201', capacity: 80, status: 'available' }
    ]
  })
})

// 创建教室接口
app.post('/classrooms', (req, res) => {
  const { name, capacity } = req.body
  res.json({
    success: true,
    message: '教室创建成功',
    data: {
      id: Date.now(),
      name,
      capacity,
      status: 'available'
    }
  })
})

// 健康检查接口
app.get('/health', (req, res) => {
  res.json({
    status: 'ok',
    uptime: process.uptime(),
    timestamp: new Date().toISOString()
  })
})

app.listen(port, () => {
  console.log(`后端测试服务器运行在 http://localhost:${port}`)
  console.log('可用的测试接口:')
  console.log('- GET /test - 测试连接')
  console.log('- GET /classrooms - 获取教室列表')
  console.log('- POST /classrooms - 创建教室')
  console.log('- GET /health - 健康检查')
}) 